package com.ictech.print.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.google.gson.Gson;
import com.ictech.print.BaseApplication;
import com.ictech.print.entity.ClientConfig;

public class FileUtil {
	
	public static void saveEid(String eid) {
		if (eid == null || eid.length() == 0) {
			return;
		}
		FileUtil.mkdirs(BaseApplication.getInnerSDCardPath() + "/pos");
		String fileName = BaseApplication.getInnerSDCardPath() + "/pos/eid";
		FileUtil.writeString(fileName, eid, "utf-8");
	}
	
	public static String getEid() {
		try {
			FileUtil.mkdirs(BaseApplication.getInnerSDCardPath() + "/pos");
			String fileName = BaseApplication.getInnerSDCardPath() + "/pos/eid";
			String eid = FileUtil.readString(fileName, "utf-8");
			if (eid == null) {
				eid = "";
			}
			return eid;
		} catch (Exception e) {
		}
		return "";
	}
	
	public static void saveConfig(ClientConfig clientConfig) {
		Gson gson = new Gson();
		saveConfig(gson.toJson(clientConfig));
	}
	
	public static void saveConfig(String config) {
		if (config == null) {
			return;
		}
		FileUtil.mkdirs(BaseApplication.getInnerSDCardPath() + "/pos");
		String fileName = BaseApplication.getInnerSDCardPath() + "/pos/clientConfig";
		FileUtil.writeString(fileName, config, "utf-8");
	}
	
	public static String loadConfig() {
		try {
			FileUtil.mkdirs(BaseApplication.getInnerSDCardPath() + "/pos");
			String fileName = BaseApplication.getInnerSDCardPath() + "/pos/clientConfig";
			String json = FileUtil.readString(fileName, "utf-8");
			if (json == null) {
				json = "";
			}
			return json;
		} catch (Exception e) {
		}
		return "";
	}
	
	public static String mkdirs(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.mkdirs();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }
	
	/**
     * 如果文件不存在，就创建文件
     * 
     * @param path 文件路径
     * @return
     */
    public static String createIfNotExist(String path) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }
 
    /**
     * 向文件中写入数据
     * 
     * @param filePath
     *            目标文件全路径
     * @param data
     *            要写入的数据
     * @return true表示写入成功  false表示写入失败
     */
    public static boolean writeBytes(String filePath, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            fos.write(data);
            fos.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
 
    /**
     * 从文件中读取数据
     * 
     * @param file
     * @return
     */
    public static byte[] readBytes(String file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            fis.read(buffer);
            fis.close();
            return buffer;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
 
    /**
     * 向文件中写入字符串String类型的内容
     * 
     * @param file
     *            文件路径
     * @param content
     *            文件内容
     * @param charset
     *            写入时候所使用的字符集
     */
    public static void writeString(String file, String content, String charset) {
        try {
        	if (content != null && content.length() > 0) {
        		byte[] data = content.getBytes(charset);
                writeBytes(file, data);
			}
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
 
    }
 
    /**
     * 从文件中读取数据，返回类型是字符串String类型
     * 
     * @param file
     *            文件路径
     * @param charset
     *            读取文件时使用的字符集，如utf-8、GBK等
     * @return
     */
    public static String readString(String file, String charset) {
        String ret = null;
        try {
        	byte[] data = readBytes(file);
        	if (data != null) {
        		ret = new String(data, charset);
			}
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }
    
}
